package com.lf.api.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lf.api.entity.User;
import com.lf.api.po.domain.UserComment;
import com.lf.api.po.domain.UserGreate;
import com.lf.api.po.domain.UserReply;
import com.lf.api.service.CommentReplyService;
import com.lf.api.vo.CommentVO;
import com.lf.api.vo.MessageResult;
import com.lf.console.Constants;

/**
 * 
 * Description: 用户评论，回复控制器
 * 		方法注释以 /user/**开头，方便权限控制
 * Copyright: Copyright (c)2012
 * Company: Administrator
 * @author: Administrator
 * @version: 1.0
 * Create at: 2016年9月7日 下午11:12:20
 *
 * Modification History:
 * Date Author Version Description
 * ---------------------------修改历史---------------------------------------
 * 2016年9月7日 Administrator 1.0
 */
@RestController
public class CommentReplyController extends BaseController{

	@Autowired
	CommentReplyService commentReplyService;
	
	@ModelAttribute("user")
	public User getUser(HttpSession session){
		//User user = (User) session.getAttribute(Constants.USER_LOGIN);
		User user = new User();
		user.setNickName("小小书童");
		user.setTrueName("王学明");
		user.setUserId(1L);
		user.setUserName("18902859746");
		return user;
	}
	
	/**
	 * 
	 * Description:添加评论
	 * Date:2016年9月7日
	 * @author Administrator
	 * @return MessageResult
	 */
	@RequestMapping("/user/comment/add")
	public MessageResult addComment(@ModelAttribute("user") User user, UserComment comment){
		if(null == comment || StringUtils.isEmpty(comment.getContent()) 
				|| null == comment.getSubjectId() || comment.getSubjectId().longValue() == 0){
			return MessageResult.PARAM_ERROR;
		}
		comment.setFromUid(user.getUserId());
		comment.setCreateTime(new Date());
		comment.setState((short)0);
		commentReplyService.saveComment(comment);
		return MessageResult.SUCCESS;
	}
	
	/**
	 * 
	 * Description:评论点赞
	 * Date:2016年9月7日
	 * @author Administrator
	 * @return MessageResult
	 */
	@RequestMapping("/user/greate")
	public MessageResult addCommentGreate(@ModelAttribute("user") User user, UserGreate greate){
		if(null == user){
			return MessageResult.SYSTEM_ERROR;
		}
		if(null == greate || null == greate.getObjectId()
				|| greate.getObjectId().longValue() == 0
				|| null == greate.getObjectType()){
			return MessageResult.PARAM_ERROR;
		}
		try{
			greate.setUserId(user.getUserId());
			//greate.setObjectType(Constants.GreateType.COMMENT);
			commentReplyService.saveGreate(greate);
		}catch(Exception e){
			e.printStackTrace();
			return MessageResult.SYSTEM_ERROR;
		}
		return MessageResult.SUCCESS;
	}
	
	/**
	 * 
	 * Description:添加回复
	 * Date:2016年9月7日
	 * @author Administrator
	 * @return MessageResult
	 */
	@RequestMapping("/user/reply/add")
	public MessageResult addReply(@ModelAttribute("user") User user, UserReply reply){
		if(null == reply || StringUtils.isEmpty(reply.getContent()) 
				|| null == reply.getCommentId() || reply.getCommentId().longValue() == 0){
			return MessageResult.PARAM_ERROR;
		}
		reply.setFromUid(user.getUserId());
		reply.setCreateTime(new Date());
		reply.setGreateNum(0);
		reply.setState(Constants.DataType.OK);
		commentReplyService.saveReply(reply);;
		return MessageResult.SUCCESS;
	}
	
	/**
	 * 
	 * Description:添加回复
	 * Date:2016年9月7日
	 * @author Administrator
	 * @return MessageResult
	 */
	@RequestMapping("/subject/comment/{subjectId}/{pageNo}")
	public MessageResult commentById(@ModelAttribute("user") User user, 
			@PathVariable Long subjectId, @PathVariable Integer pageNo){
		if(null == subjectId || subjectId.longValue() == 0 ){
			return MessageResult.PARAM_ERROR;
		}
		if(null == pageNo || pageNo.intValue() == 0){
			pageNo = 1;
		}
		List<CommentVO> comments = commentReplyService.queryCommentBySubjectId(subjectId, pageNo, Constants.DEFAULT_PAGE_SIZE);
		return MessageResult.getInstance().setCode(MessageResult.SUCCESS.getCode())
			.setMsg(MessageResult.SUCCESS.getMsg()).setData(comments);
	}
}
